﻿// Member.cs
//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ServiceModel.DomainServices.Client;

namespace BookClub.Web.DataModel {

    public partial class Member {

        public bool IsModified {
            get {
                return EntityState == EntityState.Modified;
            }
        }

        public bool IsNew {
            get {
                return (EntityState == EntityState.New) ||
                       (EntityState == EntityState.Detached);
            }
        }

        protected override void OnPropertyChanged(PropertyChangedEventArgs e) {
            base.OnPropertyChanged(e);

            if (String.CompareOrdinal(e.PropertyName, "EntityState") == 0) {
                RaisePropertyChanged("IsNew");
                RaisePropertyChanged("IsModified");
            }
        }

        public bool Validate(ValidationContext validationContext) {
            ValidationErrors.Clear();

            IServiceProvider serviceProvider = validationContext;
            IDictionary<object, object> items = null;
            if (validationContext != null) {
                items = validationContext.Items;
            }

            ValidationContext vc = new ValidationContext(this, serviceProvider, items);
            ICollection<ValidationResult> validationResults = new List<ValidationResult>();
            if (Validator.TryValidateObject(this, vc, validationResults) == false) {
                foreach (ValidationResult error in validationResults) {
                    ValidationErrors.Add(error);
                }
                return false;
            }

            return true;
        }
    }
}
